package io.gitee.bowen_lee.netty;

import java.nio.ByteBuffer;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;

/**
 * ByteBuffer vs byteBuf<br/>
 * 面向缓冲开发,可以是堆内也可以是堆外
 * 
 * @author Bowen
 * @since 2022-01-10 10:35:00
 */
public class NettyByteBuf_VS_ByteBuffer {

  public static void main(String[] args) {
    NettyByteBuf_VS_ByteBuffer nvb = new NettyByteBuf_VS_ByteBuffer();
    nvb.myBytebuf();// byteBuf
    nvb.whatByteBuffer();// byteBuffer
  }

  /*
   * netty 的 NettyBuf
   */
  public void myBytebuf() {
    // 堆外内存
    // ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(8, 20);

    // 非池化 heap堆内(jvm内存)
    // ByteBuf buf = UnpooledByteBufAllocator.DEFAULT.heapBuffer(8, 20);
    // Pooling 池化 heap堆内(jvm内存)
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.heapBuffer(8, 20);
    print(buf);

    buf.writeBytes(new byte[] { 1, 2, 3, 4 });
    print(buf);
    buf.writeBytes(new byte[] { 1, 2, 3, 4 });
    print(buf);
    buf.writeBytes(new byte[] { 1, 2, 3, 4 });
    print(buf);
    buf.writeBytes(new byte[] { 1, 2, 3, 4 });
    print(buf);
    buf.writeBytes(new byte[] { 1, 2, 3, 4 });
    print(buf);
    try {
      buf.writeBytes(new byte[] { 1, 2, 3, 4 });
    } catch (Exception e) {
      e.printStackTrace();
    }
    print(buf);

  }

  public static void print(ByteBuf buf) {
    System.out.println("buf.isReadable()    :" + buf.isReadable());
    System.out.println("buf.readerIndex()   :" + buf.readerIndex());
    System.out.println("buf.readableBytes() " + buf.readableBytes());
    System.out.println("buf.isWritable()    :" + buf.isWritable());
    System.out.println("buf.writerIndex()   :" + buf.writerIndex());
    System.out.println("buf.writableBytes() :" + buf.writableBytes());
    System.out.println("buf.capacity()  :" + buf.capacity());
    System.out.println("buf.maxCapacity()   :" + buf.maxCapacity());
    System.out.println("buf.isDirect()  :" + buf.isDirect());
    System.out.println("--------------");

  }

  /**
   * nio 的 NettyBuf
   */
  public void whatByteBuffer() {

    // ByteBuffer buffer = ByteBuffer.allocate(1024);
    ByteBuffer buffer = ByteBuffer.allocateDirect(1024);

    System.out.println("postition: " + buffer.position());
    System.out.println("limit: " + buffer.limit());
    System.out.println("capacity: " + buffer.capacity());
    System.out.println("mark: " + buffer);

    buffer.put("123".getBytes());

    System.out.println("-------------put:123......");
    System.out.println("mark: " + buffer);

    buffer.flip(); // 读写交替

    System.out.println("-------------flip......");
    System.out.println("mark: " + buffer);

    buffer.get();

    System.out.println("-------------get......");
    System.out.println("mark: " + buffer);

    buffer.compact();

    System.out.println("-------------compact......");
    System.out.println("mark: " + buffer);
    buffer.put("456".getBytes());

    System.out.println("-------------put......");
    System.out.println("mark: " + buffer);
    buffer.clear();
    System.out.println("-------------clear......");
    System.out.println("mark: " + buffer);
  }

}